package shortcircuit.engine;

import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.swing.*;
import javax.swing.event.*;

import shortcircuit.GameEngine;
import shortcircuit.Display;
import shortcircuit.graphics.MainMenuScreen;
import shortcircuit.utilities.Debug;
import shortcircuit.basics.Element;

/**
 * This class serves as the main class, and starts the main threads that completes each
 * iteration of the game through its specific GameEngine. The graphics component of this class
 * is in MainMenuScreen, which contains listeners to hold Display in check and clear its screens
 * according to user input. MainMenu holds an array of labels, detailing which modes available.
 * 
 * @author David Li
 */
public class MainMenu
{
	MainMenuScreen mainMenuScreen;
	private JButton[] modeButtons;
	private JButton howToPlayButton;
	public Display _Display;
	private final String[] buttonLabels = {"Puzzle Mode", "Endless Mode", 
			"Multiplayer Mode", "Records", "Settings"};
	
	/**
	 * Contains all of the code for the operations of the debugger
	 * 
	 * @param args Parameters passed in to the debugger
	 * @author Kevin Lewi
	 */
	public static void main(String[] args)
	{
		new MainMenu(null);
	}
	
	public MainMenu(Display theDisplay)
	{
		modeButtons = new JButton[5];
		if(theDisplay == null)
			theDisplay = new Display(null);
		_Display = theDisplay;
		_Display.clearScreens();
		mainMenuScreen = new MainMenuScreen(this);
		createMenu();
		_Display.addScreen(mainMenuScreen);


	}
	
	public void createMenu()
	{
		initializeButtons();
		
		howToPlayButton = new JButton();
		howToPlayButton.setLocation(20, 380);
		howToPlayButton.setSize(90, 20);
		howToPlayButton.addActionListener(new ActionInput());
		
		for(int index = 0; index < 2; index++){
			modeButtons[index] = new JButton(new ImageIcon(Display.modeImages[index]));
			modeButtons[index].setBorder(null);
			modeButtons[index].setContentAreaFilled(false);
			modeButtons[index].addActionListener(new ActionInput());
			modeButtons[index].setSize(400,84);
		}
		
		//displayPanel.add(theImage);
		mainMenuScreen.add(modeButtons);
	}
	
	private void initializeButtons()
	{
		MediaTracker mt = new MediaTracker(mainMenuScreen);
		Toolkit tk = mainMenuScreen.getToolkit();
		for(int i = 0; i < buttonLabels.length; i++)
		{
			JButton tempButton = new JButton();
			Image tempImage = tk.getImage("Images/" + buttonLabels[i] + ".jpg");
			mt.addImage(tempImage, i);
			tempButton.setIcon(new ImageIcon(tempImage));
			modeButtons[i] = tempButton;
		}
		try
		{
			mt.waitForAll();
		}
		catch(InterruptedException e) {}
	}
	
	/**
	 * Creates a new key file to read and write to all of the created maps.  This should be
	 * only done once.
	 * 
	 * @author Kevin Lewi
	 */
	private void generateKeyFile()
	{
		try
		{
			SecretKey key = KeyGenerator.getInstance("DES").generateKey();
			SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
			Class spec = Class.forName("javax.crypto.spec.DESKeySpec");
			DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec);
			ObjectOutputStream oos = new ObjectOutputStream(
						new FileOutputStream("key"));
			oos.writeObject(ks.getKey());;
		}
		catch(Exception e) {}
	}
	
	public class ActionInput implements ActionListener{
		public void actionPerformed(ActionEvent e){
			JButton buttonSource = (JButton) e.getSource();
			if(buttonSource == modeButtons[0]){
				//mainMenuScreen.stopAllThreads();
				_Display.clearScreens();
				new PuzzleEngine(_Display);
			}
			if(buttonSource == modeButtons[1]){
				//mainMenuScreen.stopAllThreads();
				_Display.clearScreens();
				new EndlessEngine(_Display);
			}
			if(buttonSource == modeButtons[2]){}
			if(buttonSource == modeButtons[3]){}
			if(buttonSource == modeButtons[4]){}
			if(buttonSource == howToPlayButton)
			{
			}
		}
	}
}
